<?php

/**
 *
 * @copyright	Copyright 2009-2012   庆丰裕软件工作室
 * @author	Zheng HanWei <315933614@qq.com
 * @link	http://www.qfyu.net/
 *  
 * */

    function converttoflv($in, $out) {
        $cmd = "e:/ffmpeg/bin/ffmpeg.exe -v 0 -i $in -ar 11025 $out 2>&1";
        $fh = popen($cmd, "r");
        while (fgets($fh)) { 
        }
        pclose($fh);
    }

    function getthumbnail($in, $out) {
        $cmd = "e:/ffmpeg/bin/ffmpeg.exe -i $in -pix_fmt rgb24 -ss 1 -vframes 1 -s 300x200 $out 2>&1";
        $fh = popen($cmd, "r");
        while (fgets($fh)) {
        }
        pclose($fh);
    }

    function flv_import($upfile, $fname, $title) {
             global $config;
//        $fname = preg_replace( '/\.[^\.]+$/','', basename( $fname ) ); 
        $fname=  time().rand(1000, 9999);
        $flvpath = "$fname.flv";
        $thumbpath = "$fname.gif";

        converttoflv($upfile, $config['webroot']."/movies/$flvpath");
        getthumbnail($upfile, $config['webroot']."/movies/$thumbpath");
        $array=array(
            'flvpath'=>$flvpath,
            'thumbpath'=>$thumbpath,
        );
        return $array;
    }
    
if($_POST['Submit']){
    if(empty($_POST['vid'])){

    $import=flv_import($_FILES['video']['tmp_name'], $_FILES['video']['name'], $_POST['title']);

    $time=time();
       $sql="insert into ".TRAINVID." (title,source,cat,smalltext,detail,thumb,width,height,addtime)
                values('$title','$import[flvpath]','$_POST[cat]','$_POST[smalltext]','$_POST[detail]','$import[thumbpath]',300,200,'$time')";
       $db->query($sql);
       echo "<script>location.href='module.php?m=train&s=train_video_list.php';</script>"; 
    }else  {
        $sql = "update " . TRAINVID . " set title='$_POST[title]',cat='$_POST[cat]',smalltext='$_POST[smalltext]',detail='$_POST[detail]' where id=" . $_POST['vid'];
        $db->query($sql);
        echo "<script>location.href='module.php?m=train&s=train_video_list.php';</script>";
    }
}

$sql="select * from ".TRAINCATS." where fid=0 order by sort asc";
$db->query($sql);
$cat=$db->getRows();
foreach($cat as $k=>$v){
    $sql="select * from ".TRAINCATS." where fid=".$v['id'];
    $db->query($sql);
    $cat[$k]['scat']=$db->getRows();
}


if($_GET['id']){
    $sql="select * from ".TRAINVID." where id=$_GET[id]";
$db->query($sql);
$res=$db->fetchRow();
}

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<HEAD>
<TITLE><?php echo lang_show('admin_system');?></TITLE> 
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link href="main.css" rel="stylesheet" type="text/css" />
</HEAD>
<body>
<div class="guidebox">
    培训视频添加
</div>
<form method="post" action="" enctype="multipart/form-data">
<div class="bigbox">
	<div class="bigboxhead">培训视频添加</div>
	<div class="bigboxbody">
          <table width="100%" border="0" cellspacing="0" cellpadding="0">
             <tr>
               <td width="10%">视频标题</td>
               <td><input type="text" name="title" value="<?php echo $res['title'];?>" size="60"/></td>
             </tr>
              <tr>
               <td>视频分类</td>
               <td>
                   <select name="cat">
                       <?php foreach($cat as $val){?>
                       <option <?php if($res['cat']==$val['id'])echo"selected";?> value="<?php echo $val['id']?>"><?php echo $val['name']?></option>
                       <?php foreach($val['scat'] as $list){?>
                       <option <?php if($res['cat']==$list['id'])echo"selected";?> value="<?php echo $list['id']?>">|__<?php echo $list['name']?></option>
                       <?php }?>
                       <?php }?>
                   </select>
               </td>
            </tr>
              <tr>
               <td>视频简介</td>
               <td>
                   <textarea name="smalltext" cols="57"><?php echo $res['smalltext'];?></textarea>
               </td>
            </tr>
             <tr>
               <td>选择视频文件</td>
               <td>
                   <input type="hidden" name="source" value="<?php echo $res['source'];?>"/>
                   <input type="hidden" name="thumb" value="<?php echo $res['thumb'];?>"/>
                   <input type="file" name="video"/>
               </td>
            </tr>
              <tr>
               <td>视频描述</td>
               <td>
                   <?php
			if(empty($res["detail"]))
				$res["detail"]=NULL;
			$BasePath = "../lib/fckeditor/";
			include($BasePath."fckeditor.php");	
			$fck_en = new FCKeditor('detail') ;
			if($config['language']=='cn')
				$fck_en->Config['DefaultLanguage']='zh-cn';
			else
			$fck_en->Config['DefaultLanguage']='en';
			$fck_en->BasePath    = $BasePath ;
			$fck_en->ToolbarSet  = 'Default' ;
			$fck_en->Width='100%';
			$fck_en->Height=500;
			$fck_en->Config['ToolbarStartExpanded'] = true;
			$fck_en->Value = stripslashes($res["detail"]);
			echo $fck_en->CreateHtml();
		  ?>
               </td>
            </tr>
             <tr>
               <td>&nbsp;</td>
               <td>
                   <input type="hidden" value="<?php echo $res['id']?>" name="vid"/>
                   <input class="btn" type="submit" name="Submit" value="提交"/>
               </td>
             </tr>
           </table>
	</div>
</div>
<div style="float:left; height:50px; width:80%;">&nbsp;</div>
</form>
</body>
</html>

